﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Model;
using System.Data;
using System.Data.OleDb;
namespace DAL
{
    public class AdDAL
    {
        static string conStr = ConfigurationManager.AppSettings["ConnStr"] + System.Web.HttpContext.Current.Server.MapPath("~/") + ConfigurationManager.AppSettings["dbPath"] + ";";
        public AdDAL()
        {

        }
        public static bool Add(AdInfo pi)
        {
            string sql = "insert into ad(name,img,link,click,context,createdate) values('" + pi.name + "','" + pi.img + "','" + pi.link + "'," + pi.click + ",'" + pi.context + "',now())";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Update(AdInfo pi)
        {
            string sql = "update ad set name='" + pi.name + "',img='" + pi.img + "',link='" + pi.link + "',click=" + pi.click + ",context='" + pi.context + "' where id=" + pi.id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static bool Delete(string ids)
        {
            string sql = "delete from ad where id in ("+ids+")";
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
        public static AdInfo FindOneById(int id)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select * from ad where id=" + id;
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            AdInfo pi = new AdInfo();
            while (dr.Read())
            {
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.link = DataConvert.ToString(dr["link"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
            }
            dr.Close();
            con.Close();
            return pi;
        }
        public static List<AdInfo> FindTop(int top)
        {
            OleDbConnection con = new OleDbConnection(conStr);
            string sql = "select top "+top+" * from ad";
            OleDbDataReader dr = CommonDAL.ExecuteReader(con, sql);
            List<AdInfo> list = new List<AdInfo>();
            while (dr.Read())
            {
                AdInfo pi = new AdInfo();
                pi.id = DataConvert.ToInt(dr["id"]);
                pi.name = DataConvert.ToString(dr["name"]);
                pi.img = DataConvert.ToString(dr["img"]);
                pi.link = DataConvert.ToString(dr["link"]);
                pi.click = DataConvert.ToInt(dr["click"]);
                pi.context = DataConvert.ToString(dr["context"]);
                pi.createdate = DataConvert.ToDateTime(dr["createdate"]);
                list.Add(pi);
            }
            dr.Close();
            con.Close();
            return list;
        }
        public static bool Click(int id)
        {
            string sql = "update ad set click=click+1 where id=" + id;
            int flag = CommonDAL.ExecuteNonQuery(sql);
            if (flag == 0)
            {
                return false;
            }
            else
            {
                return true;
            }
        }
    }
}
